package com.java110.mall.shop.cmd.shop;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.shop.ShopAuditDto;
import com.java110.intf.shop.IShopAuditInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.po.shop.ShopPo;
import com.java110.po.shop.ShopAuditPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Java110Cmd(serviceCode = "shop.auditEditShop")
public class AuditEditShopCmd extends Cmd {

    @Autowired
    private IShopAuditInnerServiceSMO shopAuditInnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        super.validateAdmin(context);
        Assert.hasKeyAndValue(reqJson, "auditId", "未包含审核ID");
        Assert.hasKeyAndValue(reqJson, "state", "未包含state");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        ShopAuditPo shopAuditPo = new ShopAuditPo();
        shopAuditPo.setAuditId(reqJson.getString("auditId"));
        //状态，001 提交 002 审核通过 003 审核不通过
        shopAuditPo.setState("003");
        shopAuditPo.setAuditOpinion(reqJson.getString("auditOpinion"));
        if ("1100".equals(reqJson.getString("state"))) {
            shopAuditPo.setState("002");
        }
        shopAuditInnerServiceSMOImpl.updateShopAudit(shopAuditPo);
        if (!"1100".equals(reqJson.getString("state"))) {
            return;
        }

        ShopAuditDto shopAuditDto = new ShopAuditDto();
        shopAuditDto.setAuditId(reqJson.getString("auditId"));
        List<ShopAuditDto> shopAuditDtos = shopAuditInnerServiceSMOImpl.queryShopAudits(shopAuditDto);
        if(ListUtil.isNull(shopAuditDtos)){
            return;
        }
        ShopPo shopPo = BeanConvertUtil.covertBean(shopAuditDtos.get(0),ShopPo.class);
        shopInnerServiceSMOImpl.updateShop(shopPo);
    }
}
